package org.jeecg.modules.exPingbiao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.exPingbiao.entity.ExPingbiaoExpertRecommend;
import org.jeecg.modules.exPingbiao.entity.ExPingbiaoSectionExpertConfig;
import org.jeecg.modules.exPingbiao.mapper.ExPingbiaoExpertRecommendMapper;
import org.jeecg.modules.exPingbiao.mapper.ExPingbiaoSectionExpertConfigMapper;
import org.jeecg.modules.exPingbiao.service.IExPingbiaoExpertRecommendService;
import org.jeecg.modules.exPingbiao.service.IExPingbiaoSectionExpertConfigService;
import org.jeecg.modules.exProject.entity.ExSection;
import org.jeecg.modules.exProject.service.IExSectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 评标阶段
 * @Author: jeecg-boot
 * @Date: 2024-11-09
 * @Version: V1.0
 */
@Service
public class ExPingbiaoExpertRecommendServiceImpl extends ServiceImpl<ExPingbiaoExpertRecommendMapper, ExPingbiaoExpertRecommend> implements IExPingbiaoExpertRecommendService {

    @Autowired
    private ExPingbiaoExpertRecommendMapper expertRecommendMapper;
    @Autowired
    private ExPingbiaoSectionExpertConfigMapper expertConfigMapper;
    @Autowired
    private IExPingbiaoSectionExpertConfigService sectionExpertConfigService;
    @Autowired
    private IExSectionService iExSectionService;

    @Override
    public Result<String> setRecommendExpert(String expertId, String recommendExpertId, String sectionId) {
        List<ExPingbiaoSectionExpertConfig> sectionExpertConfigList = expertConfigMapper.selectByProjectAndSectionId(null, sectionId);
        if (CollectionUtils.isEmpty(sectionExpertConfigList)) {
            List<ExPingbiaoSectionExpertConfig> expertConfigList = expertConfigMapper.selectByProjectAndSectionIdIsNull();
            List<ExPingbiaoSectionExpertConfig> insertList = expertConfigList.stream().map(e -> {
                ExPingbiaoSectionExpertConfig expertConfig = new ExPingbiaoSectionExpertConfig();
                expertConfig.setExpertId(e.getExpertId());
                expertConfig.setBidSectionId(sectionId);
                expertConfig.setGroupFlag(Boolean.FALSE);
                expertConfig.setSignFlag(Boolean.FALSE);
                return expertConfig;
            }).collect(Collectors.toList());
            sectionExpertConfigService.saveBatch(insertList);
        }
        ExPingbiaoExpertRecommend recommend = expertRecommendMapper.selectByExpertIdAndSectionId(expertId, sectionId);
        if (null != recommend) {
            return Result.error("您已经设置过了，不能重复设置");
        }
        ExPingbiaoExpertRecommend insert = new ExPingbiaoExpertRecommend();
        insert.setRecommendExpertId(recommendExpertId);
        insert.setExpertId(expertId);
        insert.setBidSectionId(sectionId);
        expertRecommendMapper.insert(insert);
        List<ExPingbiaoExpertRecommend> recommendList = expertRecommendMapper.selectBySectionId(sectionId);
        if (recommendList.size() == sectionExpertConfigList.size()) {
            Map<String, Long> recommendMap = recommendList.stream().map(ExPingbiaoExpertRecommend::getRecommendExpertId)
                    .collect(Collectors.groupingBy(e -> e, Collectors.counting()));
            Map.Entry<String, Long> maxEntry = Collections.max(recommendMap.entrySet(), Map.Entry.comparingByValue());
            String recommendGroupExpertId = maxEntry.getKey();

            ExPingbiaoSectionExpertConfig expertConfig = expertConfigMapper.selectBySectionIdAndExpertId(sectionId, recommendGroupExpertId);
            expertConfig.setGroupFlag(Boolean.TRUE);
            expertConfigMapper.updateById(expertConfig);
            ExSection updateEntity = new ExSection();
            updateEntity.setId(sectionId);
            updateEntity.setStatus("2");
            iExSectionService.updateById(updateEntity);
        }
        return Result.ok();
    }
}
